package com.extremelylargepackage;

import java.io.IOException;

import com.extremelylargepackage.entity.Button;
import com.extremelylargepackage.entity.ScrollingBackground;
import com.extremelylargepackage.sprite.AbstractSprite;

import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;

public class LevelSelectEngine extends Engine
{
	private ScrollingBackground _background;
	private Button[] _buttons;
	private Button _noLevelMsg;
	private boolean _custom;
	
	public LevelSelectEngine(ThreadCallBack thread, boolean custom) 
	{
		super(thread);
		_custom = custom;
		_background = new ScrollingBackground(GameResources.getResource("title_nc"));
	}

	@Override
	protected void onInitialize() 
	{
		String[] levels = null;
		if(_custom)
		{
			levels = FileUtils.getCustomLevelsDir(GameResources.getContext()).list();
		}
		else
		{ 
			AssetManager assets = GameResources.getAssets();
			try 
			{
				levels = assets.list("levels");
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}
		
		_buttons = new Button[levels.length];
		if(_buttons.length == 0)
		{
			_noLevelMsg = new Button(null, GameResources.SCREEN_WIDTH / 2, GameResources.SCREEN_HEIGHT * .1f);
			_noLevelMsg.setText("No levels found.");
		}
		float lastLocY = 0;
		AbstractSprite levelButton = GameResources.getResource("levelbutton_nc");
		for(int i = 0; i < levels.length; i++)
		{
			//Omit the .lvl
			if(levels[i].endsWith(".lvl"))
				levels[i] = levels[i].substring(0, levels[i].length() - 4);
			float locX = (GameResources.SCREEN_WIDTH / 2) - (levelButton.getWidth() / 2);
			float locY = lastLocY;
			if(i > 0)
				locY += levelButton.getHeight() + (GameResources.SCREEN_HEIGHT * .05f);
			lastLocY = locY;
			Button button = new Button(levelButton, locX, locY);
			_buttons[i] = button;
			_buttons[i].setText(levels[i]);
			//For a button this is a scroll speed.
			_buttons[i].setSpeed(GameResources.SCREEN_WIDTH * 2);
 		}
	}
	
	@Override
	protected void updateState(long interval)
	{
		_background.updateState(interval);
		for(int i = 0; i < _buttons.length; i++)
			_buttons[i].updateState(interval);
	}

	@Override
	public void draw(Canvas c)
	{
		_background.draw(c);
		for(int i = 0; i < _buttons.length; i++)
		{
			_buttons[i].draw(c);
		}
		if(_noLevelMsg != null)
		{
			_noLevelMsg.draw(c);
		}
	}
	
	private float _lastTouchY;
	private boolean _lastTouchMove;

	@Override
	public void onTouch(MotionEvent event) 
	{
		if(_initialized && !_paused)
		{
			long timeHeldDown = SystemClock.uptimeMillis() -  event.getDownTime();
			if(event.getAction() == MotionEvent.ACTION_UP && timeHeldDown < 500)
			{
				selectLevel(event);
			}
			else if(event.getAction() == MotionEvent.ACTION_MOVE)
			{
				scrollButtons(event);
			}
		}
	}
	
	public void selectLevel(MotionEvent event)
	{
		for(int i = 0; i < _buttons.length; i++)
		{
			if(_buttons[i].containsPoint(event.getX(), event.getY()))
			{
				if(_custom)
					_thread.startGame("level/" + _buttons[i].getText(), true);
				else
					_thread.startGame(_buttons[i].getText(), false);
				break;
			}
		}
		_lastTouchMove = false;
	}
	
	public void scrollButtons(MotionEvent event)
	{
		if(_lastTouchMove && _buttons.length > 0)
		{
			float direction = (event.getY() - _lastTouchY) * 5;
			boolean scroll;
			int idxLastButton = _buttons.length - 1;
			if(direction > 0)
			{
				float bottom = _buttons[idxLastButton].getLocY() + _buttons[idxLastButton].getHeight();
				scroll =  bottom < GameResources.SCREEN_HEIGHT;
				if(bottom + direction > GameResources.SCREEN_HEIGHT)
					direction = GameResources.SCREEN_HEIGHT - bottom;
			}
			else
			{
				scroll = _buttons[0].getLocY() > 0;
				if(_buttons[0].getLocY() + direction < 0);
					direction = 0 - _buttons[0].getLocY();
			}
			if(scroll)
			{
				for(int i = 0; i < _buttons.length; i++)
				{
					_buttons[i].setDirY(direction);
				}
			}
		}
		_lastTouchY = event.getY();
		_lastTouchMove = true;
	}

}
